package com.gmics.unifiq.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.gmics.unifiq.entity.School;
import com.google.android.gms.maps.model.LatLng;

public class SchoolFunction {

	
	JSONParser jParser = new JSONParser();
	private static final String TAG_SUCCESS = "success";

	
	public School getSchCoordByCourseName(String courseName){
		// Get LatLng of 1st Uni
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("courseName", courseName));
		// getting JSON string from URL
		String url = "http://unifiq.5kportal.com/comparison_api1/getSchoolByCourseName.php";
		JSONObject json2 = null;
		json2 = jParser.makeHttpRequest(url , "GET", params);
		Log.d("json test:", json2.toString());
		
		School s = null;
		try{ 
			JSONObject school = json2.getJSONObject("school");
			
			
			// Checking for SUCCESS TAG
			int success = json2.getInt(TAG_SUCCESS);
			
			if (success == 1) 
			{
				s = new School();
				s.setSchoolId(Integer.parseInt((String) school.get("schoolId")));
				s.setSchoolName(school.getString("schoolName"));
				s.setUniName(school.getString("uniName"));
				double lat = Double.parseDouble((String) school.get("lat"));
				double lo = Double.parseDouble((String) school.get("long"));
				
				s.setLat(lat);
				s.setLon(lo);
				
				LatLng coord = new LatLng(lat,lo);
				
				s.setCoord(coord);
				
			}
		
		} 
		catch (JSONException e) 
		{
			e.printStackTrace();
		}
		
		return s;
		
	}
	

}
